<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
	//加载登录页面
    public function login()
    {
        return view("home.login");
    }

    
    //执行前台登录
    public function doLogin(Request $request)
    {
       //执行登录判断
	   $userName = $request->input('userName');
	   $password = $request->input('password');
	   //获取对应用户信息
	   $user = \DB::table('user')->where("userName",$userName)->first();
	
	   /* //判断用户是否存在
	   if(!empty($user)){
		   //判断密码是否正确
		   //if(md5($password) == $user->password){
			   //存储session，跳转页面
			   session()->set("homeuser",$user);
			   return redirect("admin/login");
		   }else{
			   return "passwrong";
		   }
	   }else{
		   echo "none";
		   
	   } */
	   if(!empty($userName)){
		   //判断输入的用户是否存在
		   if(!empty($user)){
			   if($user->status == 0){
			   //判断密码是否正确
				   if(md5($password) == $user->password){
						return "允许登录";
					   //存储session，跳转页面
					   //session()->set("homeuser",$user);
					   //return redirect("admin/login");
					}else{
						return "passwrong";
					}
				 }else{
					return "该用户已经被冻结！";
				}
		   }else{
			   return "输入的用户名不存在";
		   }
	   }else{
		   return "没填写用户名";
	   }
	   //return back()->with('msg',"用户不存在！");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function doLoginer(Request $request)
    {
       //执行登录判断
	   $userName = $request->input('userName');
	   //获取对应用户信息
	   $user = \DB::table('user')->where("userName",$userName)->first();
	   //存储session，跳转页面
	   session()->set("homeuser",$user);
	   return redirect("/");
    } 
	
	public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
	//执行退出
    public function quit()
    {
        session()->forget('homeuser'); //从session中移除数据
		return redirect("home/login");
		//return view("home/index");//合并之后打开
    }
}
